#!/usr/bin/env python2.5
# -*- coding: UTF-8 -*- 
 
import os
from google.appengine.api import mail  
 
def getAnEmailAddress(mailboxStr, Name=""): 
    return "%s<%s@%s%s" %(Name, mailboxStr, os.environ['APPLICATION_ID'], ".appspotmail.com>") 
def sendmail(sender="Example.com Support <reply@nickmilon.appspotmail.com>",to='', subject='-', bodyTxt=None, bodyHtml =None, attachments=None, reply_to=None): 
    #message = mail.EmailMessage(sender=&quot;Example.com Support &lt;support@example.com&gt;&quot;,subject=&quot;Your account has been approved&quot;) 
    message = mail.EmailMessage(sender=sender, subject=subject, to = to) 
    #message.to = to  
    if not (bodyTxt is None):message.body = bodyTxt 
    if not (bodyHtml is None):message.html = bodyHtml 
    if not (reply_to is None):message.reply_to = reply_to  
    if not (attachments is None):message.attachments=attachments
    message.send()
    
def sendSame(inboundMsg):
    plaintext_bodies =  inboundMsg.bodies('text/plain') 
    allPlainTxtBodies = u"plaintext_bodies\n"; 
    for body in plaintext_bodies:
        # body[0] = &quot;text/plain&quot;
        # body[1] = EncodedPayload --&gt; body[1].decode()
        curBody= body[1].decode()
        if isinstance(curBody, unicode):curBody = curBody = curBody.encode('utf-8') 
        allPlainTxtBodies = allPlainTxtBodies + "\n---------------------------\n" + body[1].decode() 
    #html_bodies = inboundMsg.bodies('text/html') 
    #decoded_html =u"html >\n" 
#    for content_type, body in html_bodies:
#        curBody= body.decode()
#        #if isinstance(curBody, unicode):curBody = curBody.encode(&#39;utf-8&#39;)
#        curBody = curBody.encode(&#39;utf-8&#39;)
#        decoded_html =decoded_html+  u&#39;\n-------------------------\n&#39;+ curBody
    #rawStr="-"*80+'\n' + str(inboundMsg.original.as_string(False)) +'\n'+"-" *80 
    
    rawStr="-"*80+'\n' + str(inboundMsg.original.as_string(False)) +'\n'+"-" *80 

    newMsg= mail.EmailMessage(sender="111 Example.com Support <reply@nickmilon.appspotmail.com>",subject="Ref:" + inboundMsg.subject) 
    newMsg.to = inboundMsg.sender 
    #newMsg.body= str(dir(inboundMsg)) 
    newMsg.body= rawStr # allPlainTxtBodies + u"\n==========\n" + decoded_html 
    newMsg.send() 
